home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / proc / base.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  3.4 KB  |  158 lines

  1. /*
  2.  *  linux/fs/proc/base.c
  3.  *
  4.  *  Copyright (C) 1991, 1992 Linus Torvalds
  5.  *
  6.  *  proc base directory handling functions
  7.  */
  8.  
  9. #include <asm/segment.h>
  10.  
  11. #include <linux/errno.h>
  12. #include <linux/sched.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/string.h>
  16.  
  17. static int proc_readbase(struct inode *, struct file *, struct dirent *, int);
  18. static int proc_lookupbase(struct inode *,const char *,int,struct inode **);
  19.  
  20. static struct file_operations proc_base_operations = {
  21.     NULL,            /* lseek - default */
  22.     NULL,            /* read - bad */
  23.     NULL,            /* write - bad */
  24.     proc_readbase,        /* readdir */
  25.     NULL,            /* select - default */
  26.     NULL,            /* ioctl - default */
  27.     NULL,            /* mmap */
  28.     NULL,            /* no special open code */
  29.     NULL,            /* no special release code */
  30.     NULL            /* can't fsync */
  31. };
  32.  
  33. /*
  34.  * proc directories can do almost nothing..
  35.  */
  36. struct inode_operations proc_base_inode_operations = {
  37.     &proc_base_operations,    /* default base directory file-ops */
  38.     NULL,            /* create */
  39.     proc_lookupbase,    /* lookup */
  40.     NULL,            /* link */
  41.     NULL,            /* unlink */
  42.     NULL,            /* symlink */
  43.     NULL,            /* mkdir */
  44.     NULL,            /* rmdir */
  45.     NULL,            /* mknod */
  46.     NULL,            /* rename */
  47.     NULL,            /* readlink */
  48.     NULL,            /* follow_link */
  49.     NULL,            /* bmap */
  50.     NULL,            /* truncate */
  51.     NULL            /* permission */
  52. };
  53.  
  54. static struct proc_dir_entry base_dir[] = {
  55.     { 1,2,".." },
  56.     { 2,1,"." },
  57.     { 3,3,"mem" },
  58.     { 4,3,"cwd" },
  59.     { 5,4,"root" },
  60.     { 6,3,"exe" },
  61.     { 7,2,"fd" },
  62.     { 8,4,"mmap" },
  63.     { 9,7,"environ" },
  64.     { 10,7,"cmdline" },
  65.     { 11,4,"stat" },
  66.     { 12,5,"statm" },
  67.     { 15,4,"maps" }
  68. };
  69.  
  70. #define NR_BASE_DIRENTRY ((sizeof (base_dir))/(sizeof (base_dir[0])))
  71.  
  72. int proc_match(int len,const char * name,struct proc_dir_entry * de)
  73. {
  74.     if (!de || !de->low_ino)
  75.         return 0;
  76.     /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
  77.     if (!len && (de->name[0]=='.') && (de->name[1]=='\0'))
  78.         return 1;
  79.     if (de->namelen != len)
  80.         return 0;
  81.     return !strncmp(name, de->name, len);
  82. }
  83.  
  84. static int proc_lookupbase(struct inode * dir,const char * name, int len,
  85.     struct inode ** result)
  86. {
  87.     unsigned int pid, ino;
  88.     int i;
  89.  
  90.     *result = NULL;
  91.     if (!dir)
  92.         return -ENOENT;
  93.     if (!S_ISDIR(dir->i_mode)) {
  94.         iput(dir);
  95.         return -ENOENT;
  96.     }
  97.     ino = dir->i_ino;
  98.     pid = ino >> 16;
  99.     i = NR_BASE_DIRENTRY;
  100.     while (i-- > 0 && !proc_match(len,name,base_dir+i))
  101.         /* nothing */;
  102.     if (i < 0) {
  103.         iput(dir);
  104.         return -ENOENT;
  105.     }
  106.     if (base_dir[i].low_ino == 1)
  107.         ino = 1;
  108.     else
  109.         ino = (pid << 16) + base_dir[i].low_ino;
  110.     for (i = 0 ; i < NR_TASKS ; i++)
  111.         if (task[i] && task[i]->pid == pid)
  112.             break;
  113.     if (!pid || i >= NR_TASKS) {
  114.         iput(dir);
  115.         return -ENOENT;
  116.     }
  117.     if (!(*result = iget(dir->i_sb,ino))) {
  118.         iput(dir);
  119.         return -ENOENT;
  120.     }
  121.     iput(dir);
  122.     return 0;
  123. }
  124.  
  125. static int proc_readbase(struct inode * inode, struct file * filp,
  126.     struct dirent * dirent, int count)
  127. {
  128.     struct proc_dir_entry * de;
  129.     unsigned int pid, ino;
  130.     int i,j;
  131.  
  132.     if (!inode || !S_ISDIR(inode->i_mode))
  133.         return -EBADF;
  134.     ino = inode->i_ino;
  135.     pid = ino >> 16;
  136.     for (i = 0 ; i < NR_TASKS ; i++)
  137.         if (task[i] && task[i]->pid == pid)
  138.             break;
  139.     if (!pid || i >= NR_TASKS)
  140.         return 0;
  141.     if (((unsigned) filp->f_pos) < NR_BASE_DIRENTRY) {
  142.         de = base_dir + filp->f_pos;
  143.         filp->f_pos++;
  144.         i = de->namelen;
  145.         ino = de->low_ino;
  146.         if (ino != 1)
  147.             ino |= (pid << 16);
  148.         put_fs_long(ino, &dirent->d_ino);
  149.         put_fs_word(i,&dirent->d_reclen);
  150.         put_fs_byte(0,i+dirent->d_name);
  151.         j = i;
  152.         while (i--)
  153.             put_fs_byte(de->name[i], i+dirent->d_name);
  154.         return j;
  155.     }
  156.     return 0;
  157. }
  158.